#include "music.h"
#include <QMediaPlayer>
#include <QCoreApplication>
Music::Music()
{

}

Music::Music(QUrl url)
{
    m_url = url;
    refreshMusicInfo();

}
void Music::refreshMusicInfo(){
    QMediaPlayer player;
    player.setMedia(m_url);

    while(!player.isMetaDataAvailable()){
        QCoreApplication::processEvents();
    }
    QStringList list = player.availableMetaData();
    if(player.isMetaDataAvailable()){
        m_author = player.metaData(QStringLiteral("Author")).toStringList().join(",");
        m_name = player.metaData(QStringLiteral("Title")).toString();
        m_albumInfo = player.metaData(QStringLiteral("AlbumTitle")).toString();
        m_bitRate = player.metaData(QStringLiteral("AudioBitRate")).toInt();
        m_duration = player.duration();
    }
}
QString formatTime(qint64 timeMillSecond)
{
    qint64 tmpSec = timeMillSecond/1000;
    qint64 tmpMin = tmpSec/60;

    tmpSec -= tmpMin*60;
    return QStringLiteral("%1:%2").arg(tmpMin,2,10,QLatin1Char('0')).arg(tmpSec,2,10,QLatin1Char('0'));

}
QString Music::getMusicInfo()const{
    return m_author+"-"+m_name+"["+formatTime(m_duration)+"]";
}
